package com.lzm.ydpt.view.dialog;


import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;

import com.byt.framlib.commonutils.DisplayUtil;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.riding.OrderBean;


public class PickDialog implements View.OnClickListener {

    private static Context mContext;
    private TextView mTitle;
    private TextView tv_count_price;
    private TextView tv_add_price;
    private TextView tv_order_deliver_time;
    private TextView tv_send_order_addrss;
    private TextView tv_send_order_distance;
    private TextView tv_collect_order_addrss;
    private TextView tv_collect_order_distance;
    private TextView tv_order_todo;
    private LinearLayout ll_dialog_tips;
    private ImageView iv_check_tips;
    private Button dialog_pic_leftbtn;
    private Button dialog_pic_rightbtn;
    private Dialog mDialog;
    private View mDialogView;
    private Builder mBuilder;

    public PickDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mContext, R.style.NormalDialogStyle);
        mDialogView = View.inflate(mContext, R.layout.widget_pick_up_dialog, null);
        mTitle = mDialogView.findViewById(R.id.pic_dialog_title);
        tv_count_price = mDialogView.findViewById(R.id.tv_count_price);
        tv_add_price = mDialogView.findViewById(R.id.tv_add_price);
        tv_order_deliver_time = mDialogView.findViewById(R.id.tv_order_deliver_time);
        tv_send_order_addrss = mDialogView.findViewById(R.id.tv_send_order_addrss);
        tv_send_order_distance = mDialogView.findViewById(R.id.tv_send_order_distance);
        tv_collect_order_addrss = mDialogView.findViewById(R.id.tv_collect_order_addrss);
        tv_send_order_distance = mDialogView.findViewById(R.id.tv_collect_order_distance);
        tv_order_todo = mDialogView.findViewById(R.id.tv_order_todo);
        dialog_pic_leftbtn = mDialogView.findViewById(R.id.dialog_pic_leftbtn);
        dialog_pic_rightbtn = mDialogView.findViewById(R.id.dialog_pic_rightbtn);
        mDialogView.setMinimumHeight((int) (DisplayUtil.getScreenHeight
                (mContext) * builder.getHeight()));
        mDialog.setContentView(mDialogView);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DisplayUtil.getScreenWidth(mContext) * builder.getWidth());
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);

        initDialog(builder);
    }

    private void initDialog(Builder builder) {
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());
        mTitle.setText(builder.getTitleText());
        mTitle.setTextColor(builder.getTitleTextColor());
//        mContent.setText(builder.getContentText());
        initDialogData();
        ll_dialog_tips.setOnClickListener(this);
        dialog_pic_leftbtn.setOnClickListener(this);
        dialog_pic_rightbtn.setOnClickListener(this);
        dialog_pic_rightbtn.setText(mBuilder.getRightText());
        if (builder.getShowleft() == 1) {
            dialog_pic_leftbtn.setVisibility(View.GONE);
        } else {
            dialog_pic_leftbtn.setVisibility(View.VISIBLE);
        }
    }

    private void initDialogData() {
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.dialog_pic_leftbtn && mBuilder.getOnclickListener() != null) {
            dismiss();
            mBuilder.getOnclickListener().leftOnClick();
            return;
        }
        if (i == R.id.dialog_pic_rightbtn && mBuilder.getOnclickListener() != null) {
            dismiss();
            mBuilder.getOnclickListener().rightOnClick(iv_check_tips.isSelected());
            return;
        }
    }

    public void show() {
        mDialog.show();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    public static class Builder {

        private String titleText;//标题文本
        private int titleTextColor;//标题文本颜色
        private OrderBean orderBean;//内容文本
        private boolean isTouchOutside;
        private float height;//对话框高度
        private float width;//对话框宽度
        private PickUpClick mAppointClick;
        private int showleft;
        private String rightText;

        public Builder(Context context) {
            mContext = context;
            titleText = "温馨提示";
            titleTextColor = ContextCompat.getColor(mContext, R.color.color_272727);
            orderBean = null;
            mAppointClick = null;
            isTouchOutside = true;
            height = 0.23f;
            width = 0.65f;
            rightText = "去接单";
        }

        public String getTitleText() {
            return titleText;
        }

        public Builder setTitleText(String titleText) {
            this.titleText = titleText;
            return this;
        }

        public int getTitleTextColor() {
            return titleTextColor;
        }

        public Builder setTitleTextColor(@ColorRes int titleTextColor) {
            this.titleTextColor = ContextCompat.getColor(mContext, titleTextColor);
            return this;
        }

        public OrderBean getContentText() {
            return orderBean;
        }

        public Builder setContentText(OrderBean contentText) {
            this.orderBean = contentText;
            return this;
        }


        public PickUpClick getOnclickListener() {
            return mAppointClick;
        }

        public Builder setOnclickListener(PickUpClick onclickListener) {
            this.mAppointClick = onclickListener;
            return this;
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {

            this.isTouchOutside = isTouchOutside;
            return this;
        }


        public float getHeight() {
            return height;
        }

        public Builder setHeight(float height) {
            this.height = height;
            return this;
        }

        public int getShowleft() {
            return showleft;
        }

        public String getRightText() {
            return rightText;
        }

        public Builder setRightText(String rightText) {
            this.rightText = rightText;
            return this;
        }

        public Builder setShowleft(int showleft) {
            this.showleft = showleft;
            return this;
        }

        public float getWidth() {
            return width;
        }

        public Builder setWidth(float width) {
            this.width = width;
            return this;
        }

        public PickDialog build() {
            return new PickDialog(this);
        }
    }

    public interface PickUpClick {
        void leftOnClick();

        void rightOnClick(boolean isSave);
    }
}

